
/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
var validateStackSequences = function(pushed, popped) {
    let point = 0; // 指向 popped 序列的指针
    let stack = []; // 模拟栈
    for(let i = 0; i < pushed.length; i++) {
        stack.push(pushed[i]);
        let endItem = stack[stack.length-1];
        while(stack.length>0 && endItem === popped[point]) {
            stack.pop();
            point++;
            endItem = stack[stack.length-1];
        }
    }
    return stack.length === 0;

};